package com.morder.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.morder.Info;
import com.morder.R;
import com.morder.adapter.InfoAdapter;
import com.morder.application.BuildConfig;
import com.morder.databases.DbAdapter;
import com.morder.databases.DbHelper;
import com.morder.util.MOrderHelper;

public class InformationActivity extends Activity {

  private ListView mListView;
  private InfoAdapter mAdapter;
  private DbAdapter mDb;
  private ProgressBar mBodyProgressBar;
  private ArrayList<Info> mListInfo = new ArrayList<Info>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.information_activity);
    mDb = DbHelper.getDb();
    initView();
    
    boolean isNetworkAvailable = MOrderHelper.networkIsAvailable(getApplicationContext());
    if (!isNetworkAvailable) {//网络无连接
      Toast.makeText(this, "网络无连接，请检查网络设置", Toast.LENGTH_SHORT).show();
    }
    //new GetDataTask(0).execute();
  }

  private void initView() {
    mListView = (ListView) findViewById(R.id.list);
    
    mBodyProgressBar = (ProgressBar) findViewById(R.id.couponList_progressBar);
    mBodyProgressBar.setVisibility(View.VISIBLE);
  }
  
  private class GetDataTask extends AsyncTask<String, Integer, ArrayList<Info>> {
    int curPageIndex = 0;
    boolean isLocalData = false;// 是否是从本地读取的数据

    public GetDataTask(int page) {
      curPageIndex = page;
    }

    @Override
    protected ArrayList<Info> doInBackground(String... params) {
      boolean isNetworkAvailable = MOrderHelper.networkIsAvailable(getApplicationContext());

      int pageIndex = curPageIndex;
      if (pageIndex <= 0) {
        pageIndex = 1;
      }
      
      ArrayList<Info> listInfoLocal = mDb.getInfoListByPage(pageIndex, BuildConfig.COUPON_PAGE_SIZE);
      if (isNetworkAvailable) {// 有网络情况
        ArrayList<Info> listCouponNew = MOrderHelper.getInfoList(pageIndex);
        switch (curPageIndex) {
        case 0:// 首次加载
        case 1:// 刷新
          if (listCouponNew != null && listCouponNew.size() > 0) {
            return listCouponNew;
          }
          break;
        default:// 下拉
          break;
        }
      } else {// 无网络情况
        isLocalData = true;
        return listInfoLocal;
      }
      return null;
    }

    @Override
    protected void onPostExecute(ArrayList<Info> result) {
      System.out.println("result: " + result);

      // 网络不可用并且本地没有保存数据
      if (result == null || result.size() == 0) {// 没有新数据
        Toast.makeText(getApplicationContext(), "网络异常或服务器维护中，请重试或稍候使用！", Toast.LENGTH_SHORT).show();
        if (!MOrderHelper.networkIsAvailable(getApplicationContext()) && curPageIndex > 1) {// 下拉并且没有网络
          Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
        }
        return;
      }

      //保存到数据库
      if (!isLocalData) {
        mDb.SynchronyData2DB(result);
      }
      if (curPageIndex == 0) {// 首次加载
        mListInfo = result;

        mBodyProgressBar.setVisibility(View.GONE);
        mAdapter = new InfoAdapter(mListInfo, getApplicationContext());
        mListView.setAdapter(mAdapter);
      }
      super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
      // 主体进度条
      if (mListView.getCount() == 0) {
        mBodyProgressBar.setVisibility(View.VISIBLE);
      }
    }
  }
  
  @Override
  protected void onPause() {
    super.onPause();
    if (mBodyProgressBar != null) {
      mBodyProgressBar.setVisibility(View.GONE);
    }
  }

}
